/*
 *    Copyright (c) 2018-2025, workdata All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: workdata
 */
package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseFreezer;
import com.workcell.workdata.xthings.entity.BaseLevelGauge;
import com.workcell.workdata.xthings.entity.BaseTimeClock;
import com.workcell.workdata.xthings.mapper.BaseLevelGaugeMapper;
import com.workcell.workdata.xthings.service.BaseLevelGaugeService;
import org.springframework.stereotype.Service;

/**
 * 液位计表
 *
 * @author pigx code generator
 * @date 2023-05-16 10:44:41
 */
@Service
public class BaseLevelGaugeServiceImpl extends ServiceImpl<BaseLevelGaugeMapper, BaseLevelGauge> implements BaseLevelGaugeService {

    @Override
    public Page<BaseLevelGauge> vaguePage(BaseVo baseVo) {
        LambdaQueryWrapper<BaseLevelGauge> baseFreezerCondition = new QueryWrapper<BaseLevelGauge>().lambda()
                .and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()), i -> i
                        .like(BaseLevelGauge::getName,baseVo.getKeyWord())
                        .or()
                        .like(BaseLevelGauge::getManufacturer,baseVo.getKeyWord()))
                .eq(ObjectUtil.isNotEmpty(baseVo.getState()),BaseLevelGauge::getState,baseVo.getState())
                .and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime())
                        , i -> i.between(BaseLevelGauge::getCreateTime,baseVo.getStartTime(),baseVo.getEndTime()));
        Page<BaseLevelGauge> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        return this.page(page,baseFreezerCondition);
    }

    @Override
    public Boolean updateStateById(BaseLevelGauge baseLevelGauge) {
        Assert.isTrue(ObjectUtil.isNotEmpty(baseLevelGauge.getId()),"ID不能为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(baseLevelGauge.getState()),"状态不能为空");
        // 查询状态
        BaseLevelGauge baseOld = this.getById(baseLevelGauge.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(baseOld),"无该条记录，请核实您的操作");
        if (HardwareStateEnum.INUSE.getCode().equals(baseOld.getState())
                || HardwareStateEnum.SUSPEND.getCode().equals(baseOld.getState())){
            if (HardwareStateEnum.DRAF.getCode().equals(baseOld.getState())){
                Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
            }
        }
        return this.updateById(baseLevelGauge);
    }
}
